# environment_status.py
# Purpose: Check CPU, GPU, and key Python library status and versions

import platform
import subprocess
import sys

def check_package(package_name):
    try:
        module = __import__(package_name)
        return getattr(module, "__version__", "Unknown")
    except ImportError:
        return None

def get_gpu_info():
    try:
        result = subprocess.run(
            ["nvidia-smi", "--query-gpu=name,memory.total", "--format=csv,noheader"],
            stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True, text=True
        )
        return result.stdout.strip()
    except Exception:
        return "No NVIDIA GPU detected or nvidia-smi not available."

def main():
    env_info = {
        "Python Version": sys.version,
        "Platform": platform.platform(),
        "CPU": platform.processor(),
        "GPU": get_gpu_info(),
        "Libraries": {
            "torch": check_package("torch"),
            "cv2": check_package("cv2"),
            "numpy": check_package("numpy"),
            "sklearn": check_package("sklearn"),
        }
    }

    for key, value in env_info.items():
        if isinstance(value, dict):
            print(f"{key}:")
            for lib, ver in value.items():
                print(f"  {lib}: {ver}")
        else:
            print(f"{key}: {value}")

if __name__ == "__main__":
    main()
